package com.hadwinling.map.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.hadwinling.map.entity.Hotel;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Auther: jzhang
 * @Date: 2019/10/23 09:10
 * @Description: 主页
 */
@Controller
@RequestMapping("/hotel")
public class HotelController {

    @RequestMapping("/list")
    public String index(Model model) {
        Hotel hotel = new Hotel();
        List<Hotel> hotelList = hotel.selectList(new QueryWrapper<>());
        model.addAttribute("hotelList",hotelList);
        model.addAttribute("urlFlag","hotel");
        return "hotelList";
    }

    @RequestMapping("/form")
    public String form(Model model,Integer id) {
        Hotel hotel = new Hotel();

        if(id != null){
            hotel.setId(id);
            hotel = hotel.selectById();
        }
        model.addAttribute("hotel",hotel);
        model.addAttribute("urlFlag","hotel");
        return "hotelForm";
    }

    @PostMapping("/save")
    public String save(Hotel hotel){
        String[] coordinates = hotel.getCoordinate().split(",");
        hotel.setLongitude(new BigDecimal(coordinates[0]));
        hotel.setLatitude(new BigDecimal(coordinates[1]));
        hotel.insertOrUpdate();
        return "redirect:/hotel/list";
    }

    @ResponseBody
    @RequestMapping("/delete")
    public String delete(Hotel hotel){
        hotel.deleteById();
        return "200";
    }
}
